// JavaScript Document
((function(q){
	//重写文件模块的相关constructor
	q.file = function(file){
		
		this.file = null;//文件原生对象信息
		this.fileReader = new FileReader();//文件阅读想的相关信息
		this.isCompress = false;//是否被压缩，压缩之后会获取md5切片
		this.isSec = false;//是否被切片
		this.isUpload = false;//文件是否被上传
		this.canUpload = false;//是否可以上传
		//this.secMaxsize = 524288;//最大的大小，如果操作需要切片
		this.secSize = 512000;//是否切片
		this.seclist = [];//放置扩展切片的地方
		this.result = null;//返回类型信息
		this.proxy = new q.proxy({method:'post',dataType:'json'});//服务信息
		
		this.onCompressStart = null;//视频开始压缩触发
		this.onCompressReady = null;//当压缩准备完成的时候执行
		this.onCompressProgress = null;//压缩切片的进度
		this.onCompressEnd = null;//视频完成压缩触发
		this.onCompressErr = null;//视频压缩失败触发
		this.onUploadSuccess = null;//上传成功
		this.onUploadSecStart = null;//片段开始上传
		this.onUploadSecEnd = null;//片段上传监听成功
		this.onUploadSecErr = null;//片段上传失败的返回监听
		this.onUploadEnd = null;//文件上传完成的监听方法
		this.onUploadErr = null;//文件上传失败的监听方法
		
		//获取文件的操作方法
		this.createBynone = function(callback){
			var ths = this;
			var ele =  q('<input hidden="" type="file" />');
			q('body').append(ele);
			ele.change(function(){
				ths.file = this.files[0];
				q(ele).remove();
				ths.callback = callback;
				ths.callback&&ths.callback();
			})
			ele.click();	
		}
		
		//创建文件的相关信息
		this.create = function(){
			if('[object File]'==Object.prototype.toString.call(file)){
				this.file = file;
			}
		}
		
		//切片区域的方法
		//获取一个切片
		this.getSec = function(index){
			return this.seclist[index];	
		};
		
		//获取一个待上传的切片
		this.getSecupload = function(){
			var index = null;
			for(var i = 0;i<this.getSecnum();i++){
				if(this.seclist[i].isUpload==0){
					return i;
				}
			}
		}
		
		//获取切片的数量信息
		this.getSecnum = function(){
			return this.seclist.length;	
		}
		
		//获取已上传的切片数量
		this.getSecUploadednum = function(){
			var index = 0;
			for(var i = 0;i<this.getSecnum();i++){
				if(this.seclist[i].isUpload==1){
					index++;
				}
			}
			return index;
		}
		
		//getTmpUrl
		//获取临时的文件路径细腻系
		this.getTmpurl = function(){
			return window.URL.createObjectURL(this.file);	
		};
		
		//获取文件名
		this.getFilename = function(){
			return this.file.name;
		};
		
		//获取文件大小
		this.getFilesize = function(isByte){
			return isByte?this.file.size:k._f.filebyte2size(this.file.size);
		};
		
		//判断是不是一个视频文件
		this.isVideo = function(){
			if(this.file.type=='video/mp4'){
				return true;
			}else{
				return false;	
			}
		}
		
		//压缩的程序
		//计算md5信息
		//计算切片相关信息及整理
		this.compress = function(callback){
			var ths = this;
			ths.onCompressStart&&ths.onCompressStart();
			//使用阅读的部分
			this.fileReader.onloadend = function(){
				ths.onCompressReady&&ths.onCompressReady();
				ths.md5 = q.md5(this.result);
				ths.isCompress = true;//压缩完成
				if(ths.secSize){
					var length = this.result.length;
					//这个有切片大小的时候，证明需要切片信息
					var size = ths.secSize;
					var ender = ths.secSize;
					var staer = 0;
					var index = 0;
					ths.seclist = [];
					while(staer < length){
						var str = this.result.slice(staer,ender);
						str = q.base64encode(str);
						var file = new File([str],'sec_'+index+'_'+ths.file.name,{type:ths.file.type});
						file.md5 = q.md5(str);
						file.isUpload = 0;//是否已上传
						file.try_max = 6;//尝试6次上传全都失败的话，就判定这个文件上传失败
						file.try_num = 0;//已经尝试的次数，如果操作最大意味着上传失败
						file.index = index;
						ths.seclist.push(file);
						ths.onCompressProgress&&ths.onCompressProgress(index);
						//后续需要更新的数据信息
						index++;
						staer = ender;
						ender += size;
					}
					ths.onCompressEnd&&ths.onCompressEnd();
					ths.callback = callback;
					ths.callback();
				}
			}
			//this.fileReader.readAsText(this.file);//阅读信息
			this.fileReader.readAsBinaryString(this.file);//二进制的方式阅读
		}
		
		//上传切片信息
		this.uploadSec = function(){
			var ths = this;
			ths.onUploadSecStart&&ths.onUploadSecStart();
			var config = q.config('UPLOAD');
			var data = config.PARAMS;
			//获取一个
			//获取一个切片
			var index = this.getSecupload();
			var sec = this.getSec(index);
			//获取当前的切片ID
			if(sec){
				//这个是还有切片信息
				this.proxy.emptyDatas();
				this.proxy.setDatas(data);
				this.proxy.setData('file',sec);
				this.proxy.setData('format','base64encode');
				this.proxy.setData('file_md5',this.md5);
				this.proxy.setData('sec_md5',sec.md5);
				this.proxy.setData('sec_count',this.getSecnum());
				this.proxy.setData('sec_index',index);
				this.proxy.setUrl(config.ACTION);
				this.proxy.success = function(rs){
					ths.result = rs;
					if(rs.status==1){
						//片段上传成功
						sec.isUpload = 1;
						ths.onUploadSecEnd&&ths.onUploadSecEnd();
						setTimeout(function(){
							ths.uploadSec();	
						},1000);
					}else{
						//图片片段上传失败的，情况下
						if(sec.try_num>sec.try_max){
							//上传失败
							ths.onUploadErr&&ths.onUploadErr();
						}else{
							//更新一个，继续上传
							sec.try_num++;	
							ths.onUploadSecErr&&ths.onUploadSecErr();
							setTimeout(function(){
								ths.uploadSec();	
							},1000);
						}
					}
				}
				this.proxy.send();
			}else{
				//这个是已经上传完毕的样子
				ths.isUpload = true;
				ths.onUploadEnd&&ths.onUploadEnd();
				
			}
		}
		
		//上传整个文件
		this.uploadFile = function(){
			return $Q.alert('上传失败，文件整体上传失败');
		}
		
		//上传的操作
		this.upload = function(){
			if(!this.canUpload){ return $Q.alert('上传失败，请检查服务器端是否能正常返回');}
			this.onUploadStart&&this.onUploadStart();
			//切片的方式上传
			if(this.secSize){
				this.uploadSec();
			}else{
				this.uploadFile();
			}
		}
		
		//检测web是否可用
		this.checkWebserver = function(){
			var ths = this;
			var config = q.config('UPLOAD');
			q.ajax({
				url:config.ACTION,
				data:config.PARAMS,
				type:'post',
				dataType:'json',
				success:function(rs){
					if(rs.status==1){
						ths.config = rs.data;
						ths.canUpload = true;
					}else{
						$Q.alert(rs.message);	
						ths.canUpload = false;
					}
				},
				error:function(){
					$Q.alert('上传控件初始化失败，请检查服务器端是否能正常返回');	
					ths.canUpload = false;
				},
			});
		}
		//检测服务端可用的信息
		this.checkWebserver();
		//检测后端是否可用
		this.create();
	}
	
	//重写文件列表的相关constructor
	q.files = function(){
		
		//文件列表相关信息
		this.files = null;
		
		//获取文件的操作方法
		this.createBynone = function(callback){
			var ths = this;
			var ele =  q('<input multiple="multiple" hidden="" type="file" />');
			q('body').append(ele);
			ele.change(function(){
				ths.files = [];
				for(var i=0;i<this.files.length;i++){
					ths.files.push(new q.file(this.files[i]));
				}
				q(ele).remove();
				ths.callback = callback;
				ths.callback&&ths.callback();
			})
			ele.click();	
		};
		
		//循环的相关
		this.each = function(func){
			for(var i=0;i<this.files.length;i++){
				this.tmpFile = func;
				this.tmpFile(i,this.files[i]);	
			}	
		};
		
		//获取最早的一个
		this.first = function(){
			return (this.files[0]==undefined)?null:this.files[0];
		};
		//获取最后的一个
		this.last = function(){
			var index = this.files.length - 1;
			return (this.files[index]==undefined)?null:this.files[index];
		};
		//判断是否有下一个被上床的
		this.hasNeedUpload = function(){
			for(var i =0;i<this.files.length;i++){
				if(!this.files[i].isUpload){
					return this.files[i];
				}
			}
			return false;
		}
	}
})(this.kuoqiJs));